/*
 * File:    Get.java
 * Created: 04-Feb-2009
 * Version: $Id$
 *
 * COPYRIGHT (C) 2009, Bitgate Mobile, LLC.  All Rights Reserved.
 *
 * software@bitgatemobile.com
 */

package com.webplasm.render.tags.u;

import static com.bitgate.util.debug.Debug.debug;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import com.webplasm.context.RenderContext;
import com.webplasm.render.engine.RenderException;
import com.webplasm.render.tags.TagInterface;
import com.webplasm.render.tags.WrappedNode;
import com.webplasm.store.VariableStoreInterface;
import com.webplasm.store.VariableContainer.VariableType;

/**
 * This tag returns the contents of a variable.
 * 
 * @author kenji
 * @since Webplasm 1.0
 */
public class Get extends WrappedNode implements TagInterface {
	private static final String DESCRIPTION = "Retrieves variable contents.";

	public String getNamespace() {
		return "u";
	}
	
	public List<String> getAttributes() {
		return Arrays.asList("var", "type", "key", "column");
	}
	
	public List<String> getManagedNodes() {
		return null;
	}

	public String getDescription() {
		return DESCRIPTION;
	}

	@Override
	public StringBuffer render(RenderContext rContext) throws RenderException {
		if (elements.get("var") != null) {
			if (elements.get("var").equals("server_uuid")) {
				return new StringBuffer(UUID.randomUUID().toString());
			}
		}

		VariableType vType = VariableType.STRING;
		String variableType = elements.get("type");
		
		if (variableType != null) {
			String variableTypeString = variableType.toUpperCase();
			boolean found = false;
			
			if (variableTypeString.equals("GET") || variableTypeString.equals("POST")) {
				vType = VariableType.STRING;
			} else {
				for(VariableType varTypeEntry : VariableType.values()) {
					if (varTypeEntry.name().equals(variableTypeString)) {
						vType = varTypeEntry;
						found = true;
						break;
					}
				}
				
				if (!found) {
					debug("Variable type '" + variableTypeString + "' does not exist, defaulting to String.");
				}
			}
		}

		VariableStoreInterface vsInterface = rContext.getVariableStore().getVariable(elements.get("var"), vType);
		
		if (elements.get("key") != null) {
			if (!vsInterface.isMappable()) {
				debug("u:get exception: attempting to retrieve a key to type '" + vType + "' that is not mappable.");
				
				throw new RenderException("u:get exception: specified variable '" + elements.get("var") + "' of type '" +
						elements.get("type") + "' is not mappable.");
			}
			
			VariableStoreInterface vsKeyInterface = vsInterface.getMap().get(elements.get("key"));
			
			return new StringBuffer(vsKeyInterface.getString());
		} else if (elements.get("column") != null) {
			if (vsInterface.getType() == VariableType.SQL) {
				ResultSet rSet = (ResultSet) vsInterface.getObject();
				
				debug("u:get: Retrieve column '" + elements.get("column") + "' from SQL variable '" + elements.get("var") + "'");
				
				try {
					return new StringBuffer(rSet.getString(elements.get("column")));
				} catch(SQLException ex) {
					debug("Unable to retrieve column '" + elements.get("column") + "': " + ex.getMessage());
					throw new RenderException("u:get exception: Unable to retrieve column '" + elements.get("column") +
							"': " + ex.getMessage());
				}
			}
		} else if (variableType != null) {
			if (variableType.equalsIgnoreCase("get")) {
				try {
					debug("HTTP Get variable '" + elements.get("var") + "' = " +
							rContext.getClientContext().getVariableStore().getGetVariable(elements.get("var")));
					return new StringBuffer(rContext.getClientContext().getVariableStore().getGetVariable(elements.get("var")));
				} catch(NullPointerException ex) {
					return new StringBuffer();
				}
			} else if (variableType.equalsIgnoreCase("post")) {
				try {
					debug("HTTP Post variable '" + elements.get("var") + "' = " +
							rContext.getClientContext().getVariableStore().getPostVariable(elements.get("var")));
					return new StringBuffer(rContext.getClientContext().getVariableStore().getPostVariable(elements.get("var")));
				} catch(NullPointerException ex) {
					return new StringBuffer();
				}
			}
		}
		
		debug("Get variable '" + elements.get("var") + "' = '" + vsInterface.getString() + "'");
		
		if (vsInterface.getString() != null) {
			return new StringBuffer(vsInterface.getString());
		}
		
		return new StringBuffer();
	}
}